﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace D3DSS.Http
{
    /// <summary>
    /// Json序列化类
    /// </summary>
    public class JsonSerialize
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            var javaScriptSerializer = new JavaScriptSerializer();
            return javaScriptSerializer.Serialize(obj);
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static byte[] Serialize(object obj, Encoding encoding)
        {
            return encoding.GetBytes(Serialize(obj));
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="jsonStr">json字符窜</param>
        /// <returns></returns>
        public static T DeSerialize<T>(string jsonStr)
        {
            var javaScriptSerializer = new JavaScriptSerializer();
            return javaScriptSerializer.Deserialize<T>(jsonStr);
        }
    }
}
